﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Shop4Aggies.Data_Access;

namespace Shop4Aggies
{
    public partial class Billing : System.Web.UI.Page
    {
        public static string BILLING_OBJ_KEY = "BillingObj";

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            
            try
            {
                
                Data_Access.Billing billing = new Data_Access.Billing();
                    
                if (ddCardType.Text == "Visa")
                    billing.TypeCode = "VSA";
                else
                    billing.TypeCode = "MCD";

                billing.FirstName = tbFirstName.Text;
                billing.LastName = tbLastName.Text;
                billing.Address = tbAddress.Text;
                billing.City = tbCity.Text;
                billing.Country = tbCountry.Text;
                billing.State = tbState.Text;
                billing.Zip = tbPostalCode.Text;

                int dummy = 0;
                if (tbCCNum.Text.Length < 16 || Int32.TryParse(tbCCNum.Text.Substring(12), out dummy))
                    billing.CCLastFourDigits = dummy;
                else
                {
                    ErrorTxt.Text = "Credit Card Number Invalid.";
                    return;
                }

                if (Int32.TryParse(tbExpMonth.Text, out dummy))
                    billing.CCExpMonth = dummy;
                else
                {
                    ErrorTxt.Text = "Credit Card Expiration Month Invalid.";
                    return;
                }

                if (Int32.TryParse(tbCVV.Text, out dummy))
                    billing.CCSecurityCode = dummy;
                else
                {
                    ErrorTxt.Text = "Credit Card Security Code Invalid.";
                    return;
                }

                Session[BILLING_OBJ_KEY] = billing;

                   
                Response.Redirect("~/Checkout.aspx", false);

                
            }
            catch(Exception exp)
            {
                throw new Exception("ERROR: Processing Billing Page - " + exp.Message.ToString(), exp);
            }
            finally
            {

            }

        }
        
    }
}